/*
Copyright 2014 Aaron Batilo

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//@author: Aaron Batilo

//This class holds static methods to help handle and manipulate strings

#include "StringUtility.h"

StringUtility::StringUtility()
{
}

StringUtility::~StringUtility()
{
}

//Helper method used to split a string into a vector
vector<string> &StringUtility::split(const string &inString, char inDelimiter, vector<string> &inStringVector) {
	//Take in the string to split, and hand it to a string stream
	stringstream stringStream(inString);
	//This will hold the current element from the split string
	string currentElement;
	//While the line can validly be split
	while (getline(stringStream, currentElement, inDelimiter)) {
		//Place the currently split element into our vector
		inStringVector.push_back(currentElement);
	}
	return inStringVector;
}

//Method used to split a string into an array using a delimiter
vector<string> StringUtility::split(const string &inString, char inDelimiter) {
	//Represents all of the elements after the split
	vector<string> stringElements;
	//Use the overloaded split method
	split(inString, inDelimiter, stringElements);
	//Return the elements
	return stringElements;
}

//Helper method to convert an entire string to being lowercased
string StringUtility::toLower(string inUnconvertedString)
{
	//Here is our empty string to populate
	string lowercaseString = "";

	//Loop through the passed in string
	for (int i = 0; i < strlen(inUnconvertedString.c_str()); i++)
	{
		//Convert each character to being lowercase, then add it to our string to return
		lowercaseString += tolower(inUnconvertedString[i]);
	}

	//Return our newly built lowercase string
	return lowercaseString;
}

//Encode a string to be a char array
//Mainly used for writing to a file stream
char* StringUtility::toCharArray(string inUnconvertedString)
{
	char *encodedBytes = new char[inUnconvertedString.size() + 1];
	encodedBytes[inUnconvertedString.size()] = 0;
	memcpy(encodedBytes, inUnconvertedString.c_str(), inUnconvertedString.size());

	return encodedBytes;
}